package com.hh.bidding.service.business;

import com.hh.bidding.domain.BidNotice;
import com.hh.bidding.domain.BidTender;
import com.hh.bidding.domain.BidTenderQuotation;
import com.hh.bidding.domain.BidTenderRules;
import com.hh.bidding.service.IBidNoticeService;
import com.hh.bidding.service.IBidTenderQuotationService;
import com.hh.bidding.service.IBidTenderRulesService;
import com.hh.bidding.service.IBidTenderService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.domain.SysDictData;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class IBidTenderQuotationServiceBusiness {

    @Resource
    private IBidTenderQuotationService bidTenderQuotationService;
    @Resource
    private IBidNoticeService bidNoticeService;
    @Resource
    private IBidTenderRulesService bidTenderRulesService;
    @Resource
    private IBidTenderService bidTenderService;

    public int biddingPrice(BidTenderQuotation bidTenderQuotation){
        Integer sid = bidTenderQuotation.getSid();
        if(sid == null || sid <= 0){
            return -1;
        }
        BidNotice oldBidNotice = bidNoticeService.selectBidNoticeBySid(Long.valueOf(sid));
        if(oldBidNotice == null){
            return -1;
        }
        bidTenderQuotation.setCreateTime(DateUtils.getNowDate());
        Date uKaiEndTime = oldBidNotice.getuKaiEndTime();//竞价结束时间
        // 获取当前时间
        Date currentTime = new Date();
        // 判断 当前时间 是否在uKaiEndTime之后
        if (currentTime.after(uKaiEndTime)) {
            return -1;//竞价结束了
        }
        Integer biddingMethodRadio = 1;
        BidTenderRules bidTenderRules = bidTenderRulesService.queryBySid(Long.valueOf(sid));
        if(bidTenderRules != null){
            biddingMethodRadio = bidTenderRules.getBiddingMethodRadio();//竞标方式：1按金额从小到大排名，2按降幅从大到小排名
        }
        //排名
        if(biddingMethodRadio == 1){
            sortedByTotalPrice(bidTenderQuotation);
        }else{
            sortedByDecline(bidTenderQuotation);
        }
        int rows = bidTenderQuotationService.insert(bidTenderQuotation);

        //更新排序
        List<BidTenderQuotation> list = bidTenderQuotationService.findAllListBySid(sid);
        if(list != null && list.size() > 0){
            String tempSCode = bidTenderQuotation.getsCode();
            int currentNumber = BidRankingUtils.getSupplierCountByProject(tempSCode);
            Map<Integer,List<BidTenderQuotation>> map = list.stream().collect(Collectors.groupingBy(BidTenderQuotation::getHid));
            if(map != null && map.size() != currentNumber){
                for(BidTenderQuotation tempQuotation : list){
                    if(biddingMethodRadio == 1){
                        sortedByTotalPrice(tempQuotation);
                    }else{
                        sortedByDecline(tempQuotation);
                    }
                }
            }
            Map<Integer,Integer> tempMap =new HashMap<>();
            for(BidTenderQuotation tempQuotation : list){
                String sCode = tempQuotation.getsCode();
                Integer hid = tempQuotation.getHid();
                Integer tempSorted = tempMap.get(hid);
                //排名
                BidRankingUtils.SupplierBidInfo ranking = BidRankingUtils.getSupplierRanking(Long.valueOf(hid), sCode, tempQuotation.getCreateTime(),biddingMethodRadio);
                if (ranking != null && tempSorted == null) {
                    Integer sorted = ranking.getRank();
                    tempQuotation.setSorted(sorted);
                    //更新排名
                    int tempRows = bidTenderQuotationService.updateSorted(tempQuotation);
                    if(tempRows > 0){
                        tempMap.put(hid,sorted);
                    }
                }
            }
        }
        try{
            BidTender bidTender = new BidTender();
            bidTender.setSid(Long.valueOf(sid));
            bidTender.setsProjectState(4L);
            bidTenderService.updateBidTender(bidTender);//修改招标项目的状态为竞标中
        }catch (Exception e){
            e.printStackTrace();
        }

        return rows;
    }

    private void sortedByTotalPrice(BidTenderQuotation bidTenderQuotation){
        try{
            Integer hid = bidTenderQuotation.getHid();
            String hName = bidTenderQuotation.gethName();
            String sCode = bidTenderQuotation.getsCode();
            Double bjTotal = bidTenderQuotation.getBjTotal();
            Double bjDecline = bidTenderQuotation.getBjDecline();
            Date createTime = bidTenderQuotation.getCreateTime();
            BidRankingUtils.SupplierBidInfo  sbInfo = new BidRankingUtils.SupplierBidInfo(Long.valueOf(hid), hName, sCode, bjTotal, bjDecline,createTime);
            BidRankingUtils.updateSupplier(sbInfo);
            BidRankingUtils.BidRankingResult result1 = BidRankingUtils.rankByPrice(sCode);
            bidTenderQuotation.setSorted(result1.getSupplierInfo(Long.valueOf(hid)).getRank());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void sortedByDecline(BidTenderQuotation bidTenderQuotation){
        try{
            Integer hid = bidTenderQuotation.getHid();
            String hName = bidTenderQuotation.gethName();
            String sCode = bidTenderQuotation.getsCode();
            Double bjTotal = bidTenderQuotation.getBjTotal();
            Double bjDecline = bidTenderQuotation.getBjDecline();
            Date createTime = bidTenderQuotation.getCreateTime();
            BidRankingUtils.SupplierBidInfo  sbInfo = new BidRankingUtils.SupplierBidInfo(Long.valueOf(hid), hName, sCode, bjTotal, bjDecline,createTime);
            BidRankingUtils.updateSupplier(sbInfo);
            BidRankingUtils.BidRankingResult result1 = BidRankingUtils.rankByDeclineWithPriceWinner(sCode);
            bidTenderQuotation.setSorted(result1.getSupplierInfo(Long.valueOf(hid)).getRank());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public BidTenderQuotation findLastQuotationByHidAndSid(Integer hid, Integer sid){
        Integer biddingMethodRadio = 1;
        BidTenderRules bidTenderRules = bidTenderRulesService.queryBySid(Long.valueOf(sid));
        if(bidTenderRules != null){
            biddingMethodRadio = bidTenderRules.getBiddingMethodRadio();//竞标方式：1按金额从小到大排名，2按降幅从大到小排名
        }
        BidTenderQuotation bidTenderQuotation = bidTenderQuotationService.findLastQuotationByHidAndSid(hid,sid);
        if(bidTenderQuotation != null){
            String sCode = bidTenderQuotation.getsCode();
            //排名
            BidRankingUtils.SupplierBidInfo ranking = BidRankingUtils.getSupplierRanking(Long.valueOf(hid), sCode, bidTenderQuotation.getCreateTime(),biddingMethodRadio);
            if (ranking != null) {
                Integer sorted = ranking.getRank();
                bidTenderQuotation.setSorted(sorted);
            }
        }
        return bidTenderQuotation;
    }

    public BidTenderQuotation findLastInfoQuotationByHidAndSid(Integer hid, Integer sid){
        return bidTenderQuotationService.findLastQuotationByHidAndSid(hid,sid);
    }

    public List<BidTenderQuotation> findAllListBySid(Integer sid){
        Integer biddingMethodRadio = 1;
        BidTenderRules bidTenderRules = bidTenderRulesService.queryBySid(Long.valueOf(sid));
        if(bidTenderRules != null){
            biddingMethodRadio = bidTenderRules.getBiddingMethodRadio();//竞标方式：1按金额从小到大排名，2按降幅从大到小排名
        }
        List<BidTenderQuotation> list = bidTenderQuotationService.findAllListBySid(sid);
        if(list != null && list.size() > 0){
            for(BidTenderQuotation bidTenderQuotation : list){
                String sCode = bidTenderQuotation.getsCode();
                Integer hid = bidTenderQuotation.getHid();
                //排名
                BidRankingUtils.SupplierBidInfo ranking = BidRankingUtils.getSupplierRanking(Long.valueOf(hid), sCode, bidTenderQuotation.getCreateTime(), biddingMethodRadio);
                if (ranking != null) {
                    Integer sorted = ranking.getRank();
                    bidTenderQuotation.setSorted(sorted);
                }
            }
            //排序，需要根据sorted升序返回，如果sorted有值
            list = list.stream().filter(bidTenderQuotation -> bidTenderQuotation.getSorted() != null).sorted(Comparator.comparing(BidTenderQuotation::getSorted)).collect(Collectors.toList());
        }
        return list;
    }

}
